package com.utils;

import java.util.HashMap;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.google.gson.JsonObject;

public class SearchKuGou {
	final static String kugouServerUrl = "http://songsearch.kugou.com/song_search_v2";
	final static String kugouPlayUrl = "http://www.kugou.com/yy/index.php";
	private static String name;
	private static String singer;
	private static String url;
	private static String pic;
	private static String fileHash;
	private static String albumID;

	public static void search(String s) throws Exception{
		/*第一步搜索，获得歌曲hash值和album_id*/
		// 添加参数
		Map<String, String> params = new HashMap<String, String>();
		params.put("keyword", s);
		params.put("page", "1");
		params.put("pagesize", "1");
		params.put("clientver", "WebFilter");
		params.put("platform", "WebFilter");
		params.put("limit", "1");
		params.put("offset", "1");
		
		/*调用get请求，处理json，获得hash值*/
		String res = HttpUtils.get(kugouServerUrl, params, null);
		res = res.substring(res.indexOf("{"));
		com.alibaba.fastjson.JSONObject result = JSON.parseObject(res);// 转JSON
		String resultToString = result.toString();
		JsonObject subObject = ReadJSONKuGou.parseJSON(resultToString);
		
		fileHash = subObject.get("FileHash").getAsString();
		albumID = subObject.get("AlbumID").getAsString();

		/*第二步搜索，用hash值和album_id搜索，获得url，pic*/
		Map<String, String> paramsplay = new HashMap<String, String>();
		paramsplay.put("r", "play/getdata");
		paramsplay.put("album_id", albumID);
		paramsplay.put("hash", fileHash);
		
		String resplay = HttpUtils.get(kugouPlayUrl, paramsplay, null);
		resplay = resplay.substring(resplay.indexOf("{"));
		com.alibaba.fastjson.JSONObject resultplay = JSON.parseObject(resplay);// 转JSON
		String resultToStringPlay = resultplay.toString();
		JsonObject subObjectPlay = ReadJSONKuGouPlay.parseJSON(resultToStringPlay);
		
		url = subObjectPlay.get("play_url").getAsString();
		name = subObjectPlay.get("audio_name").getAsString();
		singer = subObjectPlay.get("author_name").getAsString();
		pic = subObjectPlay.get("img").getAsString();
		
		
	}

	public static String getName() {
		return name;
	}

	public static void setName(String name) {
		SearchKuGou.name = name;
	}

	public static String getSinger() {
		return singer;
	}

	public static void setSinger(String singer) {
		SearchKuGou.singer = singer;
	}

	public static String getUrl() {
		return url;
	}

	public static void setUrl(String url) {
		SearchKuGou.url = url;
	}

	public static String getPic() {
		return pic;
	}

	public static void setPic(String pic) {
		SearchKuGou.pic = pic;
	}

	public static String getFileHash() {
		return fileHash;
	}

	public static void setFileHash(String fileHash) {
		SearchKuGou.fileHash = fileHash;
	}

	public static String getAlbumID() {
		return albumID;
	}

	public static void setAlbumID(String albumID) {
		SearchKuGou.albumID = albumID;
	}

	public static String getKugouserverurl() {
		return kugouServerUrl;
	}

	public static String getKugouplayurl() {
		return kugouPlayUrl;
	}
	
	
	
}
